package com.example.stmp.Service.TeachClass;

import com.example.stmp.Entity.TeachClass;
import com.example.stmp.Mapper.TeachClassMapper;
import com.example.stmp.pojo.PageResult;
import com.example.stmp.pojo.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class TeachClassServiceImpl implements TeachClassService {

    @Autowired
    private TeachClassMapper teachClassMapper;

    /*
     * 新增教学班级
     * */
    @Override
    public void save(TeachClass teachClass) {

        // 设置时间
        teachClass.setCreateTime(Tool.ceareNowTime());
        teachClass.setLastUpdateTime(Tool.ceareNowTime());
        teachClassMapper.insert(teachClass);
    }

    /*
     * 修改教学班信息
     * */
    @Transactional
    public void update(TeachClass teachClass) {
        teachClass.setLastUpdateTime(Tool.ceareNowTime());
        //根据tcId查询教学班信息
        TeachClass updateTeachClass = teachClassMapper.selectTeachClassInfoById(teachClass.getTcId());
        //设置修改时间
        updateTeachClass.setLastUpdateTime(Tool.ceareNowTime());
        teachClassMapper.update(teachClass);
    }

    /*
     * 根据tcId查询教学班级信息
     * */
    @Override
    public TeachClass selectTeachClassInfoById(Long id) {

        String tcId = String.valueOf(id);
        TeachClass teachClass = teachClassMapper.selectTeachClassInfoById(tcId);
        return teachClass;
    }

    /*
     * 教学班级的分页查询
     * */
    @Override
    public PageResult queryPage(Integer pageNum, Integer pageSize) {
        Long total = teachClassMapper.selectcount();
        int startIndex = (pageNum - 1) * pageSize;
        List<TeachClass> teachClassList = teachClassMapper.pageQuery(startIndex, pageSize);
        return new PageResult(total, teachClassList);
    }

    /*
     *批量删除教学班
     * */
    @Override
    public void delete(List<Long> ids) {
        TeachClass teachClass= null;
        for (Long id : ids) {
            //获取当前状态
            String tcId = id.toString();
            int status = teachClassMapper.selectDepartStatusById(tcId);
            //更新修改时间
            teachClass = teachClassMapper.selectTeachClassInfoById(tcId);
            System.out.println(teachClass);
            teachClass.setLastUpdateTime(Tool.ceareNowTime());
            System.out.println(teachClass);
            teachClassMapper.update(teachClass);
            if(status == 1) {
                teachClassMapper.updateDepartStatus(tcId, 0);
            } else {
                teachClassMapper.updateDepartStatus(tcId, 1);
            }

        }
    }


}
